
(function (win) {
    function LuckDraw(opts) {
        this.defaultOPts = {
            pointerId: 'luckPointer',
            turntableId: 'luckTurntable',
            rotateId: 'luckTurntable',
            activeClass: 'rui-active',
            type: 'turntable',
            time: 2000,
            luckNumber: 4,
            typeNumber: 6,
            circleNumber: 10,
            success: function () {}
        };
        for (var key in opts) {
            this.defaultOPts[key] = opts[key];
        }
        // 公用参数
        this.flag = true;
    }
    // 重置全局变量
    LuckDraw.prototype._resetVariable = function () {
        this.flag = true;
        this.index = validPeople[0];
        this.timer = null;
        this.startTime = 5;
        this.rotate = this._getElem(this.defaultOPts.rotateId);
        this.toggleClass();
    }
    // 改变角标
    LuckDraw.prototype._changePrize = function(){
        var _this = this;
        var resetIndex = function(){
            for (var j = 0, len = _this.rotate.length ; j < len ; j++){
                _this.rotate[j].className = _this.defaultOPts.rotateId.split('.')[1];
            }
            var Range = validPeople.length;
            var Rand = Math.random();
            var num  = Math.floor(Rand * Range);
             _this.index  =validPeople[num];
            coutmUN[_this.index] = coutmUN[_this.index]+1;
              textPeople.push(validPeople[num]);
              validPeople.splice(num,1);
                if(validPeople.length == 0){
                    validPeople = textPeople;
                    textPeople = [];
                }
            _this.rotate[_this.index].className = _this.defaultOPts.rotateId.split('.')[1] + ' ' + _this.defaultOPts.activeClass;
                _this.timer = setTimeout(resetIndex, _this.startTime);
        }
        _this.timer = setTimeout(resetIndex, _this.startTime);
    }
    LuckDraw.prototype.toggleClass = function(){
        /**选中的框框**/
        var _this = this;
            var html ='';
            var count =0;
            var style ='';
            var wid = $(_this.defaultOPts.rotateId).width();
            for(var i=0;i<28;i++){
                if(i<=7){//横线
                    style ='left:'+parseInt(i*7+2*i)+'px;top:'+0+'px;';
                    html+='<i class="toggle_white" style='+style+'></i>';
                }
                if(i==7){
                    count = 0;
                }
                if(i>7&&i<=14){
                    count++;
                    style ='left:'+wid+'px;top:'+parseInt(count*7+2*count)+'px;';
                    html+='<i class="toggle_white" style='+style+'></i>';
                }
                if(i==14){
                    count = 0;
                }
                if(i>14&&i<=21){
                    count++;
                    style ='left:'+parseInt(wid-count*7-2*count)+'px;top:'+wid+'px;';
                    html+='<i class="toggle_white" style='+style+'></i>';
                }
                if(i==21){
                    count = 0;
                }
                if(i>21&&i<=28){
                    count++;
                    style ='left:'+0+'px;top:'+parseInt(wid-count*7-2*count)+'px;';
                    html+='<i class="toggle_white" style='+style+'></i>';
                }
            }
            $(_this.defaultOPts.rotateId).find('span').html(html);

            var lisi =$(_this.defaultOPts.rotateId).find('i');
            for(var i=0;i<lisi.length;i++){
                if(i%2==0){
                    $(lisi[i]).removeClass().addClass('toggle_white');
                }else{
                    $(lisi[i]).removeClass().addClass('toggle_yellow');
                }
            }
            setInterval(function () {
                for(var i=0;i<lisi.length;i++){
                    $(lisi[i]).toggleClass('toggle_yellow');
                }
            },300)
    }
    // 指针旋转函数
    LuckDraw.prototype._running = function () {
        var _this = this;
        setTimeout(function () {
                    _this._changePrize();
        }, 100);
    }
    // 参数测试，抛出异常
    LuckDraw.prototype._paramTesting = function(){
        if (!this.rotate[0] || this.rotate[0] === null) {
            throw this.defaultOPts.rotateId + ' is not find!';
        }
        if (this.defaultOPts.time < 1000) {

        }
    }
    // 获取元素
    LuckDraw.prototype._getElem = function (str, parEle) {
        str = str.split(" ");
        var par = [];
        parEle = parEle || document;
        var retn = [parEle];
        for (var i in str) { if (str[i].length != 0) par.push(str[i]) } //去掉重复空格
        for (var i in par) {
            if (retn.length == 0) return false;
            var _retn = [];
            for (var r in retn) {
                if (par[i][0] == "#") _retn.push(document.getElementById(par[i].replace("#", "")));
                else if (par[i][0] == ".") {
                    var tag = retn[r].getElementsByTagName('*');
                    for (var j = 0; j < tag.length; j++) {
                        var cln = tag[j].className;
                        if (cln && cln.search(new RegExp("\\b" + par[i].replace(".", "") + "\\b")) != -1) { _retn.push(tag[j]); }
                    }
                }
                else { var tag = retn[r].getElementsByTagName(par[i]); for (var j = 0; j < tag.length; j++) { _retn.push(tag[j]) } }
            }
            retn = _retn;
        }

        return retn.length == 0 || retn[0] == parEle ? false : retn;
    }
    // 控制是否点击
    LuckDraw.prototype._init = function (opts) {
            // 扩展defaultOPts对象
            for (var key in opts) {
                this.defaultOPts[key] = opts[key];
            }
            this._resetVariable();
            this._paramTesting();
            this._running();
    }

    LuckDraw.prototype._stop = function(){
        var _this = this;
            clearTimeout(_this.timer)
            _this._resetVariable();
            _this.timer = null;
            _this.defaultOPts.success(_this.index);
    }
    win.LuckDraw = LuckDraw;
})(window)